using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

namespace BlazeApps.Library.Template
{
    [Serializable()]
    public class TemplateChild : Csla.ReadOnlyBase<TemplateChild>
    {
        #region Business Properties and Methods

        //declare members
        private Guid _id = Guid.NewGuid();
        private Guid _templateid = Guid.NewGuid();
        private int _placeholdernumber = 0;
        private string _fieldname = string.Empty;
        private int _fieldtype = 0;
        private int _fieldheight = 0;
        private int _fieldwidth = 0;
        private int _editortype = 1;
        private int _templateusage = 0;
        private Boolean _ShowName = true;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid id
        {
            get
            {
                CanReadProperty("id", true);
                return _id;
            }
        }

        public Guid templateid
        {
            get
            {
                CanReadProperty("templateid", true);
                return _templateid;
            }
        }

        public int placeholdernumber
        {
            get
            {
                CanReadProperty("placeholdernumber", true);
                return _placeholdernumber;
            }
        }

        public string fieldname
        {
            get
            {
                CanReadProperty("fieldname", true);
                return _fieldname;
            }
        }

        public int fieldtype
        {
            get
            {
                CanReadProperty("fieldtype", true);
                return _fieldtype;
            }
        }

        public int fieldheight
        {
            get
            {
                CanReadProperty("fieldheight", true);
                return _fieldheight;
            }
        }

        public int fieldwidth
        {
            get
            {
                CanReadProperty("fieldwidth", true);
                return _fieldwidth;
            }
        }

        public int editortype
        {
            get
            {
                CanReadProperty("editortype", true);
                return _editortype;
            }
        }

        public int templateusage
        {
            get
            {
                CanReadProperty("templateusage", true);
                return _templateusage;
            }
        }

        public Boolean ShowName
        {
            get
            {
                CanReadProperty("ShowName", true);
                return _ShowName;
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in TemplateChild
            //AuthorizationRules.AllowRead("id", "TemplateChildReadGroup");
            //AuthorizationRules.AllowRead("templateid", "TemplateChildReadGroup");
            //AuthorizationRules.AllowRead("placeholdernumber", "TemplateChildReadGroup");
            //AuthorizationRules.AllowRead("fieldname", "TemplateChildReadGroup");
            //AuthorizationRules.AllowRead("fieldtype", "TemplateChildReadGroup");
            //AuthorizationRules.AllowRead("fieldheight", "TemplateChildReadGroup");
            //AuthorizationRules.AllowRead("fieldwidth", "TemplateChildReadGroup");
            //AuthorizationRules.AllowRead("editortype", "TemplateChildReadGroup");

        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static TemplateChild GetTemplateChild(SafeDataReader dr)
        {
            return new TemplateChild(dr);
        }

        private TemplateChild(SafeDataReader dr)
        {
            Fetch(dr);
        }
        #endregion //Factory Methods

        #region Data Access

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr)
        {
            FetchObject(dr);

            //load child object(s)
            FetchChildren(dr);
        }

        private void FetchObject(SafeDataReader dr)
        {
            _id = dr.GetGuid("id");
            _templateid = dr.GetGuid("templateid");
            _placeholdernumber = dr.GetInt32("placeholdernumber");
            _fieldname = dr.GetString("fieldname");
            _fieldtype = dr.GetInt32("fieldtype");
            _fieldheight = dr.GetInt32("fieldheight");
            _fieldwidth = dr.GetInt32("fieldwidth");
            _editortype = dr.GetInt32("editortype");
            _templateusage = dr.GetInt32("templateusage");
            _ShowName = dr.GetBoolean("showname");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
